package org.csu.jpetstore.controller;

import jakarta.servlet.http.HttpSession;
import org.csu.jpetstore.domain.Admin;
import org.csu.jpetstore.service.AdminService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;



@Controller
public class AdminController {
    @Autowired
    private AdminService adminService;

    @GetMapping("/toLogin")
    public  String toLogin(){
        return "signOn";
    }

    @RequestMapping("/login")
    public String login(String username, String password, HttpSession session){
        Admin byUsername = adminService.getByUsername(username);
        if(byUsername == null){
            session.setAttribute("msg","用户名错误");
            return "redirect:/toLogin";
        }else if(byUsername.getPassword().equals(password)){
            session.setAttribute("username",username);
            return "redirect:/main/view";
        }else {
            session.setAttribute("msg","密码错误");
            return "redirect:/toLogin";
        }
    }

    @RequestMapping("/logout")
    public String logout(HttpSession session){
        session.removeAttribute("username");
        return "redirect:/";
    }

    @RequestMapping("/register")
    public String register(Admin admin){
        adminService.insert(admin);
        return "redirect:/";
    }
}
